package edu.bbu.bibliospring.backend.service.beans;

import edu.bbu.bibliospring.backend.model.Message;
import edu.bbu.bibliospring.backend.repository.MessageRepository;
import edu.bbu.bibliospring.backend.repository.exception.BibliospringRepositoryException;
import edu.bbu.bibliospring.backend.service.MessageService;
import edu.bbu.bibliospring.backend.service.exception.BibliospringServiceException;

import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by anna on 6/12/14.
 */

@Stateless(name = "MessageService", mappedName = "ejb/MessageService")
@DependsOn({"MessageRepository", "Message"})
public class MessageServiceBean implements MessageService {

    private static final Logger LOG = Logger.getLogger(MessageService.class.getName());

    @EJB
    MessageRepository messageRepository;

    @Override
    public List<Message> getAll() throws BibliospringServiceException {
        try {
            LOG.info("MessageService - getAll");
            return messageRepository.getAll();
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Message repository - getAll()", e);
        }
    }

    @Override
    public Message getById(Long id) throws BibliospringServiceException {
        try {
            LOG.info("MessageService - getById");
            return messageRepository.getById(id);
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Message repository - getById()", e);
        }
    }

    @Override
    public Message insertMessage(Message message) throws BibliospringServiceException {
        try {
            LOG.info("MessageService - insertMessage");
            return messageRepository.save(message);
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Message repository - insertMessage()", e);
        }
    }
}
